package com.inei.reportedeverificacion.model.dao;

import java.util.ArrayList;
import java.util.List;
import com.inei.reportedeverificacion.model.connection.UtilDB;
import com.inei.reportedeverificacion.model.entity.ReporteCap2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ReporteCap2Dao implements ICRUD<ReporteCap2> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	// constructor
	public ReporteCap2Dao(Context context) {
		this.context = context;
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public ReporteCap2 buscar(ReporteCap2 o) {
		ReporteCap2 obj = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from ReporteCap2 where CLAVEVIVHOGAR=?",
				new String[] { o.getCLAVEVIVHOGAR() + "" });
		if (cursor.moveToFirst()) {
			obj = new ReporteCap2();
			obj.setCLAVEVIVHOGAR(cursor.getString(cursor
					.getColumnIndex("CLAVEVIVHOGAR")));
			obj.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
			obj.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
			obj.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
			obj.setIdgrupo(cursor.getString(cursor.getColumnIndex("idgrupo")));
			obj.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
			obj.setNRO_ORDEN(cursor.getInt(cursor.getColumnIndex("NRO_ORDEN")));
			obj.setCodigo(cursor.getInt(cursor.getColumnIndex("codigo")));
		}
		Cerrar();
		return obj;
	}

	@Override
	public List<ReporteCap2> leer() {
		List<ReporteCap2> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from ReporteCap2", null);
		if (cursor.moveToFirst()) {
			lista = new ArrayList<ReporteCap2>();
			while (!cursor.isAfterLast()) {
				ReporteCap2 o = new ReporteCap2();
				o.setCLAVEVIVHOGAR(cursor.getString(cursor
						.getColumnIndex("CLAVEVIVHOGAR")));
				o.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
				o.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
				o.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
				o.setIdgrupo(cursor.getString(cursor.getColumnIndex("idgrupo")));
				o.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
				o.setNRO_ORDEN(cursor.getInt(cursor.getColumnIndex("NRO_ORDEN")));
				o.setCodigo(cursor.getInt(cursor.getColumnIndex("codigo")));
				lista.add(o);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	public List<ReporteCap2> leerXHOGAR(String id) {
		List<ReporteCap2> lista = null;
		db = util.getReadableDatabase();
		cursor = db.rawQuery("select * from ReporteCap2 where CLAVEVIVHOGAR=?",
				new String[] { id + "" });
		if (cursor.moveToFirst()) {
			lista = new ArrayList<ReporteCap2>();
			while (!cursor.isAfterLast()) {
				ReporteCap2 o = new ReporteCap2();
				o.setCLAVEVIVHOGAR(cursor.getString(cursor
						.getColumnIndex("CLAVEVIVHOGAR")));
				o.setCcdd(cursor.getString(cursor.getColumnIndex("ccdd")));
				o.setCcpp(cursor.getString(cursor.getColumnIndex("ccpp")));
				o.setCcdi(cursor.getString(cursor.getColumnIndex("ccdi")));
				o.setIdgrupo(cursor.getString(cursor.getColumnIndex("idgrupo")));
				o.setHOGAR(cursor.getString(cursor.getColumnIndex("HOGAR")));
				o.setNRO_ORDEN(cursor.getInt(cursor.getColumnIndex("NRO_ORDEN")));
				o.setCodigo(cursor.getInt(cursor.getColumnIndex("codigo")));
				lista.add(o);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return lista;
	}

	@Override
	public int crear(ReporteCap2 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues valores = new ContentValues();
		valores.put("CLAVEVIVHOGAR", o.getCLAVEVIVHOGAR());
		valores.put("ccdd", o.getCcdd());
		valores.put("ccpp", o.getCcpp());
		valores.put("ccdi", o.getCcdi());
		valores.put("idgrupo", o.getIdgrupo());
		valores.put("HOGAR", o.getHOGAR());
		valores.put("NRO_ORDEN", o.getNRO_ORDEN());
		valores.put("codigo", o.getCodigo());

		resultado = (int) db.insert("ReporteCap2", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}
		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(ReporteCap2 o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(ReporteCap2 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();
		ContentValues valores = new ContentValues();
		valores.put("codigo", o.getCodigo());

		resultado = (int) db
				.update("ReporteCap2",
						valores,
						"CLAVEVIVHOGAR=? and NRO_ORDEN=?",
						new String[] { o.getCLAVEVIVHOGAR() + "",
								"" + o.getNRO_ORDEN() });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}
		valores.clear();
		Cerrar();
		return resultado;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		db.close();
	}
}
